package xzy.com.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.tomcat.jni.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import xzy.com.config.jwt.annotation.PassToken;
import xzy.com.entity.domain.Article;
import xzy.com.entity.domain.Menu;
import xzy.com.entity.vo.ResponseBean;
import xzy.com.service.ArticleService;

@RestController
@PassToken
@RequestMapping("/api/article")
/**
 * 
 * @author 贾子力
 * 2020年12月24日
 * @Description:帖子控制层
 */
public class ArticleController {
	@Autowired
	private ArticleService articleService;

	@PassToken
	@PostMapping("/save")//添加操作
	public ResponseBean add(@RequestBody Article article) {
		System.out.println(article);
		if (article != null) {
			Boolean result = articleService.save(article);
			if (result) {
				return new ResponseBean(HttpStatus.OK.value(), "添加成功", true);
			} else {
				return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "添加数据失败", null);
			}
		}
		return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "数据不能为空", null);
	}

	@PassToken
	@DeleteMapping("/delete/{id}")//根据id删除
	public ResponseBean delete(@PathVariable("id") Long id) {
		if (null != id) {
			boolean isDel = articleService.removeById(id);
			if (isDel) {
				return new ResponseBean(HttpStatus.OK.value(), "删除数据成功!", isDel);
			} else {
				return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "删除数据失败", null);
			}
		}
		return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "id不能为空", null);
	}

	// 分页处理
	@PassToken
	@GetMapping("/page")
	public ResponseBean listPages(@RequestParam("pages") Integer pages, @RequestParam("size") Integer size) {
		Page<Article> page = new Page<>(pages, size);
		QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByAsc("id");
		Page<Article> pages1 = articleService.page(page, queryWrapper);
		IPage<Article> articlePage = articleService.findArticleType(pages1, queryWrapper);

		// 查询菜单
		List<Menu> menuList = articleService.findMenuByType();
		if (menuList == null) {
			return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "菜单不存在", null);
		}

		Map<String, Object> map = new HashMap<>();
		map.put("list", articlePage.getRecords());
		map.put("recordTotal", articlePage.getTotal());
		map.put("pageTotal", articlePage.getPages());
		map.put("menuList", menuList);
		return new ResponseBean(HttpStatus.OK.value(), "分页请求成功", map);
	}

	// 搜索查询分页处理
	@PassToken
	@GetMapping("/search")
	public ResponseBean searchListPages(@RequestParam(value="title",required = false) String title, @RequestParam("pages") Integer pages,
			@RequestParam("size") Integer size) {
		Page<Article> page = new Page<>(pages, size);
		QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByAsc("id");
		if (null != title) {
			queryWrapper.like("title", title);
		}
		Page<Article> articlePage = articleService.page(page, queryWrapper);
		/*
		 * Page<Article> pages1 = articleService.page(page, queryWrapper); if (null !=
		 * title) { queryWrapper.like("title", title); }
		 */
//		System.out.println("册："+title);
//		IPage<Article> articlePage = articleService.findArticleType(pages1, queryWrapper);
		// 查询菜单

		System.out.println("测试："+articlePage.getRecords());
		List<Menu> menuList = articleService.findMenuByType();
		if (menuList == null) {
			return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "菜单不存在", null);
		}

		Map<String, Object> map = new HashMap<>();
		map.put("list", articlePage.getRecords());
		map.put("recordTotal", articlePage.getTotal());
		map.put("pageTotal", articlePage.getPages());
		map.put("menuList", menuList);
		return new ResponseBean(HttpStatus.OK.value(), "分页请求成功", map);
	}

	// 根据id查询
	@PassToken
	@GetMapping("/findById")
	public ResponseBean findById(@RequestParam("id") Long id) {
		if (null != id) {
			Article article = articleService.getById(id);
			if (article != null) {
				return new ResponseBean(HttpStatus.OK.value(), "查询成功!", article);
			} else {
				return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "查询失败", null);
			}
		}
		return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "id不能为空", null);
	}

	// 修改
	@PassToken
	@PutMapping("/update")
	public ResponseBean updateUser(@RequestBody Article article) {
		if (null != article) {// 核验数据
			boolean result = articleService.updateById(article);
			if (result) {
				new ResponseBean(HttpStatus.OK.value(), "修改成功", true);
			} else {
				return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "修改失败", null);
			}
		}
		return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "修改失败", null);
	}

	// 删除
	@PassToken
	@DeleteMapping("/delete")
	public ResponseBean delete(@RequestParam("id") Integer id) {
		ResponseBean rb = new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "删除失败", null);
		if (null != id) {
			boolean result = articleService.removeById(id);
			if (result) {
				return new ResponseBean(HttpStatus.OK.value(), "删除成功", true);
			} else {
				return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "删除失败", null);
			}
		}
		return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "id不能为空", null);
	}

	// 传入的json数组key=list，为一组整数索引
	@PassToken
	@DeleteMapping("/deleteAll")
	public ResponseBean deleteAll(@RequestBody JSONObject jsonObject) {
		ResponseBean rb = new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "批量删除失败", null);
		JSONArray jsonArray = jsonObject.getJSONArray("list");
		List<Integer> idList = jsonArray.toJavaList(Integer.class);
		if (!idList.isEmpty()) {
			boolean result = articleService.removeByIds(idList);
			if (result) {
				return new ResponseBean(HttpStatus.OK.value(), "删除成功", true);
			} else {
				return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "删除失败", null);
			}
		}
		return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "id不能为空", null);
	}
}
